import { topCategory } from '@/utils/constants'
import { getHeaderCategory } from '@/api/category'

// 分类模块
export default {
  namespaced: true,
  state () {
    return {
      list: topCategory.map(item => ({ name: item }))
    }
  },
  mutations: {
    setList (state, value) {
      state.list = value
    },
    show (state, id) {
      const currCategory = state.list.find(item => item.id === id)
      currCategory.open = true
    },
    hide (state, id) {
      const currCategory = state.list.find(item => item.id === id)
      currCategory.open = false
    }
  },
  actions: {
    async getList ({ commit }) {
      const { result } = await getHeaderCategory()
      // 给一级分类加一个控制二级菜单 显示与隐藏的字段
      result.forEach(item => {
        item.open = false
      })
      // 获取数据后 通过mutations修改数据
      commit('setList', result)
    }
  }
}
